<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\User;

class CreateUser implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;



    /**
     * 任务尝试次数
     *
     * @var int
     */
    public $tries = 5;

    /*
    * 参数
    */
    protected $data;
    
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($data)
    {
        $this->data = $data;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $count = User::where('openid',$this->data['openid'])->count();
        if($count<=0){
            User::create([
                'agent_id'=>$this->data['agent_id'],
                'code'=>$this->data['code'],
                'url'=>$this->data['url'],
                'ua'=>$this->data['ua'],
                'last_ip'=>$this->data['last_ip'],
                'openid'=>$this->data['openid'],
                'state'=>1
            ]);
        }
    }
}
